<template>
  <aside class="category-menu">
    <div class="category-header">
<!--      <h3 class="category-title">-->
<!--        <i class="category-icon">📂</i>-->
<!--        商品分类-->
<!--      </h3>-->
<!--      <div class="category-count">{{ categories.length }} 个分类</div>-->
    </div>

<!--    <div class="category-search">-->
<!--      <input-->
<!--        v-model="searchKeyword"-->
<!--        type="text"-->
<!--        placeholder="搜索分类..."-->
<!--        class="search-input"-->
<!--      />-->
<!--    </div>-->

    <div class="category-list">
      <div
        v-for="item in filteredCategories"
        :key="item.categoryId"
        class="category-item"
        :class="{ active: selectedCategory?.categoryId === item.categoryId }"
        @mouseenter="() => onSelect(item)"
        @click="onSelect(item)"
      >
        <div class="category-icon">
          <span>🏷️</span>
        </div>
        <div class="category-info">
          <div class="category-name">{{ item.categoryName }}</div>
        </div>
        <div class="category-arrow">›</div>
      </div>
    </div>

    <div v-if="loading" class="loading">
      <div class="loading-spinner"></div>
      <span>加载中...</span>
    </div>

    <div v-if="error" class="error-state">
      <div class="error-icon">❌</div>
      <div class="error-text">{{ error }}</div>
      <button @click="refreshCategories" class="retry-btn">重试</button>
    </div>

    <div v-if="!loading && !error && filteredCategories.length === 0" class="empty-state">
      <div class="empty-icon">📭</div>
      <div class="empty-text">暂无分类数据</div>
    </div>
  </aside>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, nextTick } from 'vue'
import { getCategoryPage } from '../api/product'

interface Category {
  categoryId: number
  categoryName: string
  sortOrder?: number
  createTime?: string
  updateTime?: string
}

const categories = ref<Category[]>([])
const selectedCategory = ref<Category | null>(null)
const searchKeyword = ref('')
const loading = ref(false)
const error = ref<string | null>(null)

const filteredCategories = computed<Category[]>(() => {
  if (!searchKeyword.value) return categories.value
  return categories.value
})

onMounted(async () => {
  await nextTick()
  await loadCategories()
})

async function loadCategories() {
  if (loading.value) return
  loading.value = true
  error.value = null
  try {
    const response = await getCategoryPage()
    let categoryData: Category[] = []
    if (response && typeof response === 'object') {
      if (Array.isArray(response)) {
        categoryData = response
      } else if (response.data && Array.isArray(response.data)) {
        categoryData = response.data
      } else if ((response as any).records && Array.isArray((response as any).records)) {
        categoryData = (response as any).records
      } else if ((response as any).list && Array.isArray((response as any).list)) {
        categoryData = (response as any).list
      }
    }
    if (categoryData.length > 0) {
      localStorage.setItem('primaryCategories', JSON.stringify(categoryData))
    }
    categories.value = categoryData
    if (categoryData.length > 0 && !selectedCategory.value) {
      selectedCategory.value = categoryData[0]
    }
  } catch (err: any) {
    const local = localStorage.getItem('primaryCategories')
    if (local) {
      try {
        categories.value = JSON.parse(local)
      } catch {}
    }
    error.value = err?.message || '加载分类数据失败'
    categories.value = categories.value || []
  } finally {
    loading.value = false
  }
}

function onSelect(item: Category) {
  selectedCategory.value = item
  // 可在此处 emit 或跳转
}

// 手动刷新
async function refreshCategories() {
  await loadCategories()
}

defineExpose({
  loadCategories,
  refreshCategories,
  selectedCategory
})
</script>

<style scoped>
.category-menu {
  width: 260px;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 4px 24px rgba(80, 80, 120, 0.08);
  padding: 12px 0;
  overflow: hidden;
  min-height: 400px;
}

.category-header {
  padding: 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.category-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
}

.category-icon {
  font-size: 22px;
  margin-right: 16px;
}

.category-count {
  font-size: 12px;
  opacity: 0.8;
}

.category-search {
  padding: 16px 20px;
}

.search-input {
  width: 100%;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  font-size: 14px;
  backdrop-filter: blur(10px);
}

.search-input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.search-input:focus {
  outline: none;
  background: rgba(255, 255, 255, 0.2);
}

.category-list {
  max-height: 600px;
  overflow-y: auto;
}

.category-item {
  display: flex;
  align-items: center;
  padding: 18px 24px;
  cursor: pointer;
  border-radius: 12px;
  margin: 4px 8px;
  transition: background 0.2s, color 0.2s;
  position: relative;
  background: transparent;
}

.category-item:hover,
.category-item.active {
  background: linear-gradient(90deg, #e0e7ff 0%, #f3e8ff 100%);
  color: #5b21b6;
}

.category-info {
  flex: 1;
  min-width: 0;
}

.category-name {
  font-weight: 600;
  font-size: 16px;
  white-space: normal;
  overflow: visible;
  text-overflow: unset;
}

.category-arrow {
  font-size: 18px;
  opacity: 0.5;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
  gap: 12px;
}

.loading-spinner {
  width: 24px;
  height: 24px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
  gap: 12px;
}

.error-icon {
  font-size: 32px;
  opacity: 0.6;
}

.error-text {
  font-size: 14px;
  opacity: 0.8;
}

.retry-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.retry-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
  gap: 12px;
}

.empty-icon {
  font-size: 32px;
  opacity: 0.6;
}

.empty-text {
  font-size: 14px;
  opacity: 0.8;
}

@media (max-width: 600px) {
  .category-menu {
    width: 100vw;
    border-radius: 0;
    min-height: unset;
    box-shadow: none;
  }
  .category-item {
    padding: 14px 12px;
    margin: 2px 0;
    font-size: 15px;
  }
}
</style>
